<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once 'excel_reader2.php';

class Tambahan extends MX_Controller {
  function __construct() {
    $this->load->library('template');
    $this->load->library('form_eksternal');
    $this->load->library('global_models');
    $this->load->library('mobile_detect');
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->helper('path');
    $this->load->helper('url');
    $this->load->model('outlet/maktifitas');
    $this->load->model('outlet/moutlet');
    $this->load->library('parser');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->library('pagination');
    $this->menu = $this->cek();
   
  }
  function activity($id_outlet){
    if($this->input->post()){
      $pst = $this->input->post();
      
        $month = (int)date("m", strtotime(str_replace("_","-", $pst['tanggal'])));
        $year = (int)date("Y", strtotime(str_replace("_","-", $pst['tanggal'])));
        $day = (int)date("j", strtotime(str_replace("_","-", $pst['tanggal']))); 
        $id_actifity_report = $this->global_models->get_field("m_actifity_report", "id_actifity_report", array("month" => $month, "year" => $year, "id_outlet" => $pst['id_outlet']));
        $id_report = $this->global_models->get_field("d_day_actifity_report", "id_actifity_report", array("id_actifity_report" => $id_actifity_report, "tanggal" => $day));
        
        if(!empty($id_report)){
        $kirim = array(
            "id_actifity_report"          => $id_actifity_report,
            "tanggal"                     => $pst['tanggal'],
            "service"                     => $pst['service'],
            "service_belanja"             => $pst['service_belanja'],
            "belanja"                     => $pst['belanja'],
            "p_service"                   => $pst['p_service'],
            "p_service_plus"              => $pst['p_service_plus'],
            "p_belanja_plus"              => $pst['p_belanja_plus'],
            "p_belanja"                   => $pst['p_belanja'],
            "total_hpp"                   => $pst['total_hpp'],
            "update_by_users"             => $this->session->userdata("id"),
        );
        $id_day_actifity_report = $this->global_models->update("d_day_actifity_report", array("id_day_actifity_report" => $pst['id_day_actifity_report']), $kirim);
      }
      else{
         $kirim = array(
            "id_actifity_report"          => $id_actifity_report,
            "tanggal"                     => $day,
            "service"                     => $pst['service'],
            "service_belanja"             => $pst['service_belanja'],
            "belanja"                     => $pst['belanja'],
            "p_service"                   => $pst['p_service'],
            "p_service_plus"              => $pst['p_service_plus'],
            "p_belanja_plus"              => $pst['p_belanja_plus'],
            "p_belanja"                   => $pst['p_belanja'],
            "total_hpp"                   => $pst['total_hpp'],
            "create_by_users"             => $this->session->userdata("id"),
            "create_date"                 => date("Y-m-d H:i:s")
        );
        $id_day_actifity_report = $this->global_models->insert("d_day_actifity_report", $kirim);
      }
      redirect("outlet/milik");
    }
    $this->template->build('tambahan/add-activity', 
      array(
            'message'     => "",
            'url'     => base_url()."themes/srabon/",
            'id_outlet'    => $id_outlet,
            'title_table'    => "Activity Record",
          ));
    $this->template
      ->set_layout('default')
      ->build('tambahan/add-activity');
  }
  
  function import_activity($id_actifity_report){
   
    if($this->session->userdata("outlet") <= 0){
      redirect("outlet/milik");
    }
   
    $list = $this->global_models->get_query("
      SELECT A.*, B.name AS activity
      FROM d_outlet_file_import AS A
      LEFT JOIN m_actifity_report AS B ON A.id_actifity_report = B.id_actifity_report
      WHERE A.id_actifity_report = {$id_actifity_report}");
    $this->template->title('Sistem', "Outlet");
    
    $view = "tambahan/import-activity";
    
    $this->template->build($view, 
      array(
            'message'     => "",
            'url'     => base_url()."themes/srabon/",
            'data'    => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "outlet",
            'id_actifity_report' => $id_actifity_report,
            'title_table'    => "File Activity ".$this->global_models->get_field("m_outlet", "name", array("id_outlet" => $this->session->userdata('outlet'))),
          ));
    $this->template
      ->set_layout('default')
      ->build($view);
  }
  
  function get_data($id_outlet, $tanggal) {
    $date = (int)date("d", strtotime(str_replace("_","-", $tanggal)));
    $month = (int)date("m", strtotime(str_replace("_","-", $tanggal)));
    $year = (int)date("Y", strtotime(str_replace("_","-", $tanggal)));
    $id_actifity_report = $this->global_models->get_field("m_actifity_report", "id_actifity_report", array("month" => $month, "year" => $year, "id_outlet" => $id_outlet));
    $data = $this->global_models->get("d_day_actifity_report", array("id_actifity_report" => $id_actifity_report, "tanggal" => $date));
//    print_r($id_outlet);die;
    $hasil = array(
        "service"               => $data[0]->service,
        "service_belanja"       => $data[0]->service_belanja,
        "belanja"               => $data[0]->belanja,
        "p_service"             => $data[0]->p_service,
        "p_service_plus"        => $data[0]->p_service_plus,
        "p_belanja_plus"        => $data[0]->p_belanja_plus,
        "p_belanja"             => $data[0]->p_belanja,
        "total_hpp"             => $data[0]->total_hpp,
        "id_day_actifity_report"             => $data[0]->id_day_actifity_report,
    );
    print json_encode($hasil);
    die;
  }
  
  function import_file($id_actifity_report){
    if($this->input->post()){
      $pst = $this->input->post();
      $config['upload_path'] = './files/activity/';
      $config['allowed_types'] = '*';
      
      $this->load->library('upload', $config);
      
      if($_FILES['document']['name']){
        if ( ! $this->upload->do_upload('document')){
          $error = array('error' => $this->upload->display_errors());
          print $this->upload->display_errors();
          die;
        }
        else{
          $data = array('upload_data' => $this->upload->data());
          $kirim = array(
              "id_actifity_report"        => $pst['id_actifity_report'],
              "document"                  => $data['upload_data']['file_name'],
              "create_by_users"           => $this->session->userdata("id"),
              "create_date"               => date("Y-m-d H:i:s")
          );
          $id_outlet_file_import = $this->global_models->insert("d_outlet_file_import", $kirim);
        }
      }
      
      redirect("outlet/tambahan/import-activity/".$id_actifity_report);
    }
    $this->template->build('tambahan/import-file', 
      array(
            'message'     => "",
            'url'     => base_url()."themes/srabon/",
            'id_actifity_report'    => $id_actifity_report,
            'title_table'    => "Import File",
          ));
    $this->template
      ->set_layout('default')
      ->build('tambahan/import-file');
  }
  
  function execution_import($id_outlet_file_import){
    $data = $this->global_models->get("d_outlet_file_import", array("id_outlet_file_import" => $id_outlet_file_import));
//    $this->debug($data);
    $excel = new Spreadsheet_Excel_Reader("files/activity/".$data[0]->document);
    
    foreach($excel->sheets[0]['cells'][2] AS $k_tanggal => $v_tanggal){
      if($k_tanggal >= 3){
//        $this->debug($v_tanggal."|".$k_tanggal);
//        $this->debug($excel->sheets[0]['cells'][3][$k_tanggal]);
//        $this->debug($excel->sheets[0]['cells'][4][$k_tanggal]);
//        $this->debug($excel->sheets[0]['cells'][5][$k_tanggal]);
//        $this->debug($excel->sheets[0]['cellsInfo'][6][$k_tanggal]['raw']);
//        $this->debug($excel->sheets[0]['cellsInfo'][7][$k_tanggal]['raw']);
//        $this->debug($excel->sheets[0]['cellsInfo'][8][$k_tanggal]['raw']);
//        $this->debug($excel->sheets[0]['cellsInfo'][9][$k_tanggal]['raw']);
//        $this->debug($excel->sheets[0]['cellsInfo'][10][$k_tanggal]['raw']);
        $id_day_actifity_report = $this->global_models->get_field("d_day_actifity_report", "id_day_actifity_report", 
                array("tanggal" => $v_tanggal, "id_actifity_report" => $data[0]->id_actifity_report));
//        $this->debug($id_day_actifity_report);
        if($id_day_actifity_report){
          $kirim_update = array(
              "service"                     => $excel->sheets[0]['cells'][3][$k_tanggal],
              "service_belanja"             => $excel->sheets[0]['cells'][4][$k_tanggal],
              "belanja"                     => $excel->sheets[0]['cells'][5][$k_tanggal],
              "p_service"                   => $excel->sheets[0]['cellsInfo'][6][$k_tanggal]['raw'],
              "p_service_plus"              => $excel->sheets[0]['cellsInfo'][7][$k_tanggal]['raw'],
              "p_belanja_plus"              => $excel->sheets[0]['cellsInfo'][8][$k_tanggal]['raw'],
              "p_belanja"                   => $excel->sheets[0]['cellsInfo'][9][$k_tanggal]['raw'],
              "total_hpp"                   => $excel->sheets[0]['cellsInfo'][10][$k_tanggal]['raw'],
              "update_by_users"             => $this->session->userdata("id"),
          );
          $this->global_models->update("d_day_actifity_report", array("id_day_actifity_report" => $id_day_actifity_report), $kirim_update);
        }
        else{
          $kirim[] = array(
              "id_actifity_report"          => $data[0]->id_actifity_report,
              "tanggal"                     => $v_tanggal,
              "service"                     => $excel->sheets[0]['cells'][3][$k_tanggal],
              "service_belanja"             => $excel->sheets[0]['cells'][4][$k_tanggal],
              "belanja"                     => $excel->sheets[0]['cells'][5][$k_tanggal],
              "p_service"                   => $excel->sheets[0]['cellsInfo'][6][$k_tanggal]['raw'],
              "p_service_plus"              => $excel->sheets[0]['cellsInfo'][7][$k_tanggal]['raw'],
              "p_belanja_plus"              => $excel->sheets[0]['cellsInfo'][8][$k_tanggal]['raw'],
              "p_belanja"                   => $excel->sheets[0]['cellsInfo'][9][$k_tanggal]['raw'],
              "total_hpp"                   => $excel->sheets[0]['cellsInfo'][10][$k_tanggal]['raw'],
              "create_by_users"             => $this->session->userdata("id"),
              "create_date"                 => date("Y-m-d H:i:s")
          );
        }
      }
    }
    
    $this->global_models->insert_batch("d_day_actifity_report", $kirim);
//    die;
//    $this->debug($kirim);
//    $this->debug($excel->sheets[0]['cellsInfo'], true);
//    $this->debug($excel, true);
    redirect("outlet/aktifitas/detail/".$data[0]->id_actifity_report);
  }
  
  public function add_target($id_outlet, $year = ""){
    
    if(!$year)
      $year = date("Y");
    
    if($this->input->post()){
      $pst = $this->input->post();
//      $this->debug($pst, true);
      foreach($pst['service'] AS $k => $service){
        $id_actifity_report = $this->global_models->get_field("m_actifity_report", "id_actifity_report", 
                array("year" => $year, "month" => $k+1, "id_outlet" => $pst['id_outlet']));
//        $this->debug($id_actifity_report);
        if(!$id_actifity_report){
//          $this->debug($id_actifity_report);
//          die;
          $tanggal = $year."-".($k+1)."-01";
          $id_actifity_report = $this->global_models->insert("m_actifity_report", 
              array(
                  "id_outlet" =>  $pst['id_outlet'],
                  "name"      =>  "Laporan ".date("F", strtotime($tanggal))." ".$year,
                  "month"     =>  ($k+1),
                  "day_in_month"     =>  date("t", strtotime($tanggal)),
                  "year"      =>  $year,
                  "create_by_users" => $this->session->userdata("id"),
                  "create_date" =>  date("Y-m-d H:i:s"),
                  ));
        }
        $id_day_actifity_report = $this->global_models->get_field("d_day_actifity_report", "id_day_actifity_report", 
                array("id_actifity_report" => $id_actifity_report, "tanggal" => 0));
        if($id_day_actifity_report){
          $kirim = array(
              "id_actifity_report"        => $id_actifity_report,
              "tanggal"                   => 0,
              "service"                   => $service,
              "service_belanja"           => $pst['service_belanja'][$k],
              "belanja"                   => $pst['belanja'][$k],
              "rata_service"              => $pst['rp_service'][$k],
              "rata_belanja"              => $pst['rp_belanja'][$k],
              "hpp"                       => $pst['hpp'][$k],
              "update_by_users"           => $this->session->userdata("id"),
          );
          $id_day_actifity_report = $this->global_models->update("d_day_actifity_report", 
                  array("id_day_actifity_report" => $id_day_actifity_report), $kirim);
        }
        else{
          $kirim = array(
              "id_actifity_report"        => $id_actifity_report,
              "tanggal"                   => 0,
              "service"                   => $service,
              "service_belanja"           => $pst['service_belanja'][$k],
              "belanja"                   => $pst['belanja'][$k],
              "rata_service"              => $pst['rp_service'][$k],
              "rata_belanja"              => $pst['rp_belanja'][$k],
              "hpp"                       => $pst['hpp'][$k],
              "create_by_users"           => $this->session->userdata("id"),
              "create_date"               =>  date("Y-m-d H:i:s"),
          );
          $id_day_actifity_report = $this->global_models->insert("d_day_actifity_report", $kirim);
        }
      }
    }
    
    $outlet = $this->global_models->get("m_outlet", array("id_outlet" => $id_outlet));
    
    $data_target = $this->global_models->get_query("
      SELECT A.*, C.service, C.service_belanja, C.belanja, C.rata_service, C.rata_belanja, C.hpp
      FROM (SELECT 1 AS month
      UNION SELECT 2 AS month
      UNION SELECT 3 AS month
      UNION SELECT 4 AS month
      UNION SELECT 5 AS month
      UNION SELECT 6 AS month
      UNION SELECT 7 AS month
      UNION SELECT 8 AS month
      UNION SELECT 9 AS month
      UNION SELECT 10 AS month
      UNION SELECT 11 AS month
      UNION SELECT 12 AS month
      ) AS A
      LEFT JOIN m_actifity_report AS B ON (B.month = A.month AND B.id_outlet = $id_outlet AND B.year = $year)
      LEFT JOIN d_day_actifity_report AS C ON (B.id_actifity_report = C.id_actifity_report AND C.tanggal = 0)
      GROUP BY A.month
      ");
//    $data_target = $this->global_models->get_query("
//      SELECT A.*
//      FROM d_day_actifity_report AS A
//      WHERE tanggal = 0 AND id_actifity_report IN (SELECT id_actifity_report FROM m_actifity_report WHERE year = $year)
//      ");
    
//    $this->debug($data_target, true);
    
    $this->template->title('Sistem', "Outlet");
    
    $view = "tambahan/target";
    $this->template->build($view, 
      array('message' => "",
            'url'     => base_url()."themes/srabon/",
            'data'    => $list,
            'data_target'        => $data_target,
            'title_table'    => "Outlet ".$outlet[0]->name." Data Table",
            'outlet'    => $outlet,
            'year'      => $year,
          ));
    $this->template
      ->set_layout('default')
      ->build($view);
  }
  
  function export_xls_target($id_outlet, $year){
    $data_target = $this->global_models->get_query("
      SELECT A.*, C.service, C.service_belanja, C.belanja, C.rata_service, C.rata_belanja, C.hpp
      FROM (SELECT 1 AS month
      UNION SELECT 2 AS month
      UNION SELECT 3 AS month
      UNION SELECT 4 AS month
      UNION SELECT 5 AS month
      UNION SELECT 6 AS month
      UNION SELECT 7 AS month
      UNION SELECT 8 AS month
      UNION SELECT 9 AS month
      UNION SELECT 10 AS month
      UNION SELECT 11 AS month
      UNION SELECT 12 AS month
      ) AS A
      LEFT JOIN m_actifity_report AS B ON (B.month = A.month AND B.id_outlet = $id_outlet AND B.year = $year)
      LEFT JOIN d_day_actifity_report AS C ON (B.id_actifity_report = C.id_actifity_report AND C.tanggal = 0)
      GROUP BY A.month
      ");
    
//    $this->debug($data_target, true);
    
    $objPHPExcel = $this->phpexcel;
      $objPHPExcel->getProperties()->setCreator("Nugroho Budhi Santoso")
							 ->setLastModifiedBy("NuBuSa")
							 ->setTitle("Unis BTN Data")
							 ->setSubject("BTN")
							 ->setDescription("budhi_nusa@yahoo.com")
							 ->setKeywords("btn nubusa")
							 ->setCategory("Nubusa");

      $objPHPExcel->setActiveSheetIndex(0);
      
      $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Bulan');
      $objPHPExcel->getActiveSheet()->setCellValue('B1', 'Service');
      $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Service dan Belanja');
      $objPHPExcel->getActiveSheet()->setCellValue('D1', 'Belanja');
      $objPHPExcel->getActiveSheet()->setCellValue('E1', 'Rata-rata Rupiah Service');
      $objPHPExcel->getActiveSheet()->setCellValue('F1', 'Rata-rata Rupiah Belanja');
      $objPHPExcel->getActiveSheet()->setCellValue('G1', '% HPP');
      
      $objPHPExcel->getActiveSheet()->getStyle('A1:G1')->applyFromArray(
          array(
            'font'    => array(
              'bold'      => true
            ),
            'alignment' => array(
              'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
              'allborders'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
            ),
            'fill' => array(
              'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
            )
          )
      );
      
      $objPHPExcel->getActiveSheet()->getStyle('A1:T4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
      $objPHPExcel->getActiveSheet()->getStyle('A1:T4')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
      
      $data = array(
          1 => "Januari",
          2 => "Februari",
          3 => "Maret",
          4 => "April",
          5 => "Mei",
          6 => "Juni",
          7 => "Juli",
          8 => "Agustus",
          9 => "September",
          10 => "Oktober",
          11 => "Nopember",
          12 => "Desember",
      );
      $no = 2;
      foreach($data_target AS $dt){
        $objPHPExcel->getActiveSheet()->setCellValue('A'.$no, $data[$dt->month]);
        $objPHPExcel->getActiveSheet()->setCellValue('B'.$no, $dt->service);
        $objPHPExcel->getActiveSheet()->setCellValue('C'.$no, $dt->service_belanja);
        $objPHPExcel->getActiveSheet()->setCellValue('D'.$no, $dt->belanja);
        $objPHPExcel->getActiveSheet()->setCellValue('E'.$no, $dt->rata_service);
        $objPHPExcel->getActiveSheet()->setCellValue('F'.$no, $dt->rata_belanja);
        $objPHPExcel->getActiveSheet()->setCellValue('G'.$no, $dt->hpp);
        $no++;
      }
      
      $objPHPExcel->getActiveSheet()->getStyle('A2:G'.$no)->applyFromArray(
          array(
            'font'    => array(
              'bold'      => false
            ),
            'alignment' => array(
              'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
              'allborders'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
            ),
            'fill' => array(
              'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
            )
          )
      );
      
      $abjad = array("A", "B", "C", "D", "E", "F", "G");
      foreach ($abjad AS $colom){
        $objPHPExcel->getActiveSheet()->getColumnDimension($colom)->setAutoSize(true);
      }
      $objPHPExcel->setActiveSheetIndex(0);
      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename="target-eksport-'.date("Y m d").'".xls');
      header('Cache-Control: max-age=0');
      $objWriter = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel5');
      $objWriter->save('php://output');die;
  }
  
  public function add_pica($id_actifity_report){
    if($this->input->post()){
      $pst = $this->input->post();
      if($pst['id_detail']){
        $kirim = array(
            "title"           => $pst['title'],
            "code"            => $pst['code'],
            "update_by_users" => $this->session->userdata("id"),
        );
        $id_hrm_department = $this->global_models->update("hrm_department", array("id_hrm_department" => $id_hrm_department), $kirim);
      }
      else{
        $kirim = array(
            "id_pic_users"        => $this->session->userdata("id"),
            "id_actifity_report"  => $id_actifity_report,
            "problem"             => $pst['problem'],
            "corrective"          => $pst['corrective'],
            "tanggal_buat"        => $pst['tanggal_buat'],
            "kategori"            => $pst['kategori'],
            "kategori_view"       => $pst['kategori_view'],
            "sumber_info_view"    => $pst['sumber_info_view'],
            "due_date"            => $pst['due_date'],
            "status_view"         => $pst['status_view'],
            "id_pic_pica"         => $pst['id_pic_pica'],
            "create_by_users"     => $this->session->userdata("id"),
            "create_date"         => date("Y-m-d H:i:s")
        );
        $id_history = $this->global_models->insert("d_history", $kirim);
      }
      if($id_history){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data tidak tersimpan');
      }
      redirect("outlet/aktifitas/history/".$id_actifity_report);
    }
    else{
    
      $this->template->build("tambahan/add-pica",
        array(
              'url'     => base_url()."themes/srabon/",
              'menu'        => 'outlet/milik',
              'title'       => "Add PICA",
            ));
      $this->template
        ->set_layout('default')
        ->build("tambahan/add-pica");
    }
	}
  
  function add_pic_pica($id_pic_pica = 0){
    if($this->input->post()){
      $pst = $this->input->post();
      if($pst['id_detail']){
        $kirim = array(
            "name"            => $pst['name'],
            "email"           => $pst['email'],
            "kategori"        => $pst['kategori'],
            "id_users"        => $pst['id_users'],
            "update_by_users" => $this->session->userdata("id"),
        );
        $id_pic_pica = $this->global_models->update("m_pic_pica", array("id_pic_pica" => $pst['id_detail']), $kirim);
      }
      else{
        $kirim = array(
            "name"            => $pst['name'],
            "email"           => $pst['email'],
            "kategori"        => $pst['kategori'],
            "id_users"        => $pst['id_users'],
            "create_by_users" => $this->session->userdata("id"),
            "create_date"     => date("Y-m-d H:i:s")
        );
        $id_pic_pica = $this->global_models->insert("m_pic_pica", $kirim);
      }
      if($id_pic_pica){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data tidak tersimpan');
      }
      redirect("outlet/tambahan/pic-pica");
    }
    else{
      $data = $this->global_models->get("m_pic_pica", array("id_pic_pica" => $id_pic_pica));
      $view = "tambahan/add-pic-pica";
      $this->template->build($view, 
        array('message' => "",
              'url'     => base_url()."themes/srabon/",
              'data'    => $data,
              'menu_drop'   => $this->menu,
              'menu'        => "settings",
              'title'    => "PIC PICA",
            ));
      $this->template
        ->set_layout('default')
        ->build($view);
    }
  }
  
  function pic_pica(){
    $list = $this->global_models->get("m_pic_pica");
//    $this->debug($list, true);
    $this->template->title('Sistem', "Outlet");
    $view = "tambahan/pic-pica";
    $this->template->build($view, 
      array('message' => "",
            'url'     => base_url()."themes/srabon/",
            'data'    => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "settings",
            'title_table'    => "PIC PICA",
            'foot'    => ""
          ));
    $this->template
      ->set_layout('default')
      ->build($view);
  }
  
  function auto_pic_pica($kategori){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    $items = $this->global_models->get_query("
      SELECT *
      FROM m_pic_pica
      WHERE 
      (LOWER(name) LIKE '%{$q}%') AND kategori = {$kategori}
      LIMIT 0,10
      ");
    if(count($items) > 0){
      foreach($items as $tms){
        $result[] = array(
            "id"    => $tms->id_pic_pica,
            "label" => $tms->name,
            "value" => $tms->name,
        );
      }
    }
    else{
      $result[] = array(
          "id"    => 0,
          "label" => "No Found",
          "value" => "No Found",
      );
    }
    echo json_encode($result);
    die;
  }
  
  function auto_activity($id_outlet){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    $items = $this->global_models->get_query("
      SELECT *
      FROM m_actifity_report
      WHERE
      (LOWER(name) LIKE '%{$q}%') AND id_outlet = {$id_outlet}
      LIMIT 0,10
      ");
    if(count($items) > 0){
      foreach($items as $tms){
        $result[] = array(
            "id"    => $tms->id_actifity_report,
            "label" => $tms->name,
            "value" => $tms->name,
        );
      }
    }
    else{
      $result[] = array(
          "id"    => 0,
          "label" => "No Found",
          "value" => "No Found",
      );
    }
    echo json_encode($result);
    die;
  }
  
  function auto_outlet(){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    $items = $this->global_models->get_query("
      SELECT *
      FROM m_outlet
      WHERE
      (LOWER(name) LIKE '%{$q}%')
      LIMIT 0,10
      ");
    if(count($items) > 0){
      foreach($items as $tms){
        $result[] = array(
            "id"    => $tms->id_outlet,
            "label" => $tms->name,
            "value" => $tms->name,
        );
      }
    }
    else{
      $result[] = array(
          "id"    => 0,
          "label" => "No Found",
          "value" => "No Found",
      );
    }
    echo json_encode($result);
    die;
  }
  
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */